package com.yanqu.road.server.cmd.activity.treasure;

import com.yanqu.road.entity.activity.treasure.UserTreasureActivityData;
import com.yanqu.road.pb.treasureactivity.TreasureActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.treasure.TreasureActivityModule;
import com.yanqu.road.entity.activity.treasure.TreasureResult;
import com.yanqu.road.server.manger.activity.treasure.TreasureActivityMgr;
import com.yanqu.road.server.pb.TreasureActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ACTIVITY_TREASURE_FIND, desc = "灵狐寻宝")
public class TreasureFindCmd extends AbstractCommand {
    int activityId;
    int findType;
    TreasureResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        TreasureActivityProto.TreasureFindReqMsg reqMsg = TreasureActivityProto.TreasureFindReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        findType = reqMsg.getFindType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        result = player.getModule(TreasureActivityModule.class).findTreasure(activityId, findType);
    }

    @Override
    public void sendData(GamePlayer player) {
        TreasureActivityProto.TreasureFindRespMsg.Builder resp = TreasureActivityProto.TreasureFindRespMsg.newBuilder();
        resp.setRet(result.getRet());
        if (result.getRet() == 0) {
            resp.setReward(result.getRewards());
            resp.addAllFindStep(result.getFindStepList());
            resp.addAllPlaceId(result.getPlaceIdList());
            UserTreasureActivityData userData = result.getUserTreasureActivityData();
            if (userData != null) {
                resp.setUserTreasureData(TreasureActivityPb.parseUserDataTempBuilder(userData, TreasureActivityMgr.getUserDisperseGoldTimes(userData), TreasureActivityMgr.getPlaceInfo(activityId, userData.getRouteIndex())));
            }
        }

        player.sendPacket(ClientProtocol.U_ACTIVITY_TREASURE_FIND, resp);
    }
}
